/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package quanlinganhang.view;

import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import quanlinganhang.dao.Validator;
import quanlinganhang.dao.AccountTypeDAO;
import javax.swing.text.JTextComponent;
import quanlinganhang.dao.LoginDAO;
import quanlinganhang.dto.AccountTypeDTO;

/**
 *
 * @author phamduong
 */
public class AddAccountType extends javax.swing.JDialog {

    /**
     * Creates new form AddAccountType
     */
    public AddAccountType(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        setLocationRelativeTo(LoginDAO.frmMain);
        setVisible(true);
    }

    /**
     * Edit account type
     *
     * @param parent
     * @param modal
     * @param id
     * @param name
     * @param interest
     * @param deadline
     * @param note
     */
    public AddAccountType(java.awt.Frame parent, boolean modal, long id,
            String name, float interest, int deadline, String note) {
        super(parent, modal);
        initComponents();
        txtId.setText(Long.toString(id));
        txtId.setEditable(false);
        txtInterest.setText(Float.toString(interest));
        txtDeadline.setText(Integer.toString(deadline));
        txtName.setText(name);
        txtNote.setText(note);
        jbtAdd.setText("Lưu");
        setLocationRelativeTo(LoginDAO.frmMain);
        setVisible(true);

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        txtId = new javax.swing.JTextField();
        txtName = new javax.swing.JTextField();
        txtInterest = new javax.swing.JTextField();
        jbtAdd = new javax.swing.JButton();
        btCancel = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        txtDeadline = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        txtNote = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(0, 102, 153));
        jLabel1.setText("Thêm loại tài khoản");

        jLabel2.setText("Mã loại tài khoản");

        jLabel3.setText("Tên loại tài khoản");

        jLabel4.setText("Lãi xuất");

        jbtAdd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/quanlinganhang/resources/add.png"))); // NOI18N
        jbtAdd.setText("Thêm loại tài khoản");
        jbtAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtAddActionPerformed(evt);
            }
        });

        btCancel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/quanlinganhang/resources/no.png"))); // NOI18N
        btCancel.setText("Hủy");
        btCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btCancelActionPerformed(evt);
            }
        });

        jLabel5.setText("Kỳ hạn");

        jLabel6.setText("Ghi chú");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(86, 86, 86)
                        .addComponent(jLabel1))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(29, 29, 29)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel2)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel4))
                                .addGap(49, 49, 49)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jbtAdd)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(btCancel)
                                        .addGap(18, 18, 18))
                                    .addComponent(txtId)
                                    .addComponent(txtName)
                                    .addComponent(txtInterest, javax.swing.GroupLayout.PREFERRED_SIZE, 294, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel5)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(txtDeadline, javax.swing.GroupLayout.PREFERRED_SIZE, 294, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel6)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(txtNote, javax.swing.GroupLayout.PREFERRED_SIZE, 294, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap(48, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(27, 27, 27)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(22, 22, 22)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(txtInterest, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(27, 27, 27)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(txtDeadline, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(27, 27, 27)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(txtNote, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbtAdd)
                    .addComponent(btCancel))
                .addContainerGap(26, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jbtAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtAddActionPerformed
        //if add new account type form
        if (jbtAdd.getText().equalsIgnoreCase("Thêm loại tài khoản")) {
            JTextComponent[] tcp = {txtId, txtName, txtInterest, txtDeadline, txtNote};
            if (!Validator.validateComponents(tcp)) {
                return;
            }
            String idtext = txtId.getText();
            if (Validator.isEmpty(idtext)) {
                return;
            }

            String name = txtName.getText();
            if (Validator.isEmpty(name)) {
                return;
            }

            String theInterest = txtInterest.getText();
            if (Validator.isEmpty(theInterest)) {
                return;
            }

            String theDeadline = txtDeadline.getText();
            if (Validator.isEmpty(theDeadline)) {
                return;
            }

            String note = txtNote.getText();
            if (Validator.isEmpty(note)) {
                return;
            }

            try {
                long id = Long.parseLong(idtext);
                float interest = Float.parseFloat(theInterest);
                int deadline = Integer.parseInt(theDeadline);
                //UserDAO.AddUser(new UserDTO(id, name, pass, isAdmin));
                AccountTypeDAO.AddAccountType(new AccountTypeDTO(id, name, interest, deadline, note));
                new CustomMessageDialog(null, true, "Thành công", "Thêm loại tài khoản thành công", CustomMessageDialog.DONE);
                dispose();
                //            ManageUser.reloadTable();
            } catch (NumberFormatException ex) {
                new CustomMessageDialog(null, true, "Lỗi nhập liệu",
                        "Có lỗi trong quá trình nhập liệu, vui lòng kiểm tra lại giá trị", CustomMessageDialog.MESSAGE);

            } catch (ClassNotFoundException ex) {
                new CustomMessageDialog(null, true, "Lỗi cơ sở dữ liệu",
                        "Có lỗi trong quá trình thêm loại tài khoản", CustomMessageDialog.MESSAGE);
            } catch (SQLException ex) {
                new CustomMessageDialog(null, true, "Lỗi cơ sở dữ liệu",
                        "Có lỗi trong quá trình thêm loại tài khoản", CustomMessageDialog.MESSAGE);
            }
        }// if edit account type
        else {
            JTextComponent[] tcp = {txtId, txtName, txtInterest, txtDeadline, txtNote};
            if (!Validator.validateComponents(tcp)) {
                return;
            }
            String idtext = txtId.getText();
            if (Validator.isEmpty(idtext)) {
                return;
            }

            String name = txtName.getText();
            if (Validator.isEmpty(name)) {
                return;
            }

            String theInterest = txtInterest.getText();
            if (Validator.isEmpty(theInterest)) {
                return;
            }

            String theDeadline = txtDeadline.getText();
            if (Validator.isEmpty(theDeadline)) {
                return;
            }

            String note = txtNote.getText();
            if (Validator.isEmpty(note)) {
                return;
            }

            try {
                long id = Long.parseLong(idtext);
                float interest = Float.parseFloat(theInterest);
                int deadline = Integer.parseInt(theDeadline);
                //UserDAO.AddUser(new UserDTO(id, name, pass, isAdmin));
                AccountTypeDAO.UpdateAccountType(new AccountTypeDTO(id, name, interest, deadline, note));
                new CustomMessageDialog(null, true, "Thành công", "Cập nhật loại tài khoản thành công", CustomMessageDialog.DONE);
                dispose();
                //            ManageUser.reloadTable();
            } catch (NumberFormatException ex) {
                new CustomMessageDialog(null, true, "Lỗi nhập liệu",
                        "Có lỗi trong quá trình nhập liệu, vui lòng kiểm tra lại giá trị", CustomMessageDialog.MESSAGE);

            } catch (ClassNotFoundException ex) {
                new CustomMessageDialog(null, true, "Lỗi cơ sở dữ liệu",
                        "Có lỗi trong quá trình cập nhật loại tài khoản", CustomMessageDialog.MESSAGE);
            } catch (SQLException ex) {
                new CustomMessageDialog(null, true, "Lỗi cơ sở dữ liệu",
                        "Có lỗi trong quá trình cập nhật loại tài khoản", CustomMessageDialog.MESSAGE);
            }
        }

    }//GEN-LAST:event_jbtAddActionPerformed

    private void btCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCancelActionPerformed
        // TODO add your handling code here:
        dispose();
    }//GEN-LAST:event_btCancelActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(AddAccountType.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(AddAccountType.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(AddAccountType.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(AddAccountType.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                AddAccountType dialog = new AddAccountType(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btCancel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JButton jbtAdd;
    private javax.swing.JTextField txtDeadline;
    private javax.swing.JTextField txtId;
    private javax.swing.JTextField txtInterest;
    private javax.swing.JTextField txtName;
    private javax.swing.JTextField txtNote;
    // End of variables declaration//GEN-END:variables
}
